<?php

namespace app\services\eid;


use think\facade\Db;
use think\facade\Env;
use think\facade\Cache;
use think\facade\Event;
use think\facade\Snowflake;
use app\services\BaseServices;
use crmeb\services\CacheService;
use app\dao\worker\RepairWorkerDao;
use crmeb\exceptions\AdminException;
use think\db\exception\{DataNotFoundException, DbException, ModelNotFoundException};
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Faceid\V20180301\FaceidClient;
use TencentCloud\Faceid\V20180301\Models\GetEidTokenRequest;

/**
 *
 * Class UserServices
 * @package app\services\eid
 * @mixin RepairWorkerDao
 */
class EidTokenServices extends BaseServices
{
    /**
     * UserServices constructor.
     * @param RepairWorkerDao $dao
     */
    public function __construct(RepairWorkerDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @return mixed
     */
    public function getEidToken()
    {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            $cred = new Credential(Env::get('TencentCloud.secretId'), Env::get('TencentCloud.secretKey'));
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("faceid.tencentcloudapi.com");

            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            $client = new FaceidClient($cred, "", $clientProfile);

            // 实例化一个请求对象,每个接口都会对应一个request对象
            $req = new GetEidTokenRequest();

            $params = array(
                "MerchantId" => Env::get('TencentCloud.MerchantId')
            );
            $req->fromJsonString(json_encode($params));

            // 返回的resp是一个GetEidTokenResponse的实例，与请求对象对应
            $resp = $client->GetEidToken($req);

            // 输出json格式的字符串回包
//            print_r($resp->toJsonString());
            return json_decode($resp->toJsonString(), true);
        } catch (TencentCloudSDKException $e) {
//            echo $e;
            throw new AdminException($e);
        }

    }
}
